package com.itchu.customer;

import com.itchu.common.domain.PageResult;
import com.itchu.system.domain.Song;
import com.itchu.system.domain.User;
import com.itchu.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/user")
public class vUserController {

    @Autowired
    private UserService userService;


    /**
     * 检查用户名或者电话是否已被注册
     * @param type  1是用户名， 2是手机
     * @param value  用户名或者手机号
     * @return
     */
    @GetMapping("/register/checkOnly")
    public ResponseEntity<Boolean> checkNameAndPhone(
            @RequestParam(value = "type") Long type,
            @RequestParam(value = "value") String value
    ) {
        Boolean b = userService.checkNameAndPhone(type, value);
        return ResponseEntity.ok(b);
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @PutMapping("/save")
    public ResponseEntity<User> updateUser(User user) {

        User u = userService.updateUser(user);
        return ResponseEntity.ok(u);
    }

    /**
     * 向阿里云发送验证码操作
     * @return
     */
    @PostMapping("/code")
    public ResponseEntity<Void> sendCode(@RequestParam("phone") String phone) {
        userService.sendCode(phone);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 注册业务
     * @param user  用户的信息
     * @param code  验证码
     * @return
     */
    @PostMapping("/register")
    public ResponseEntity<Boolean> register(User user ,@RequestParam("code") String code) {

        Boolean b = userService.register(user,code);

        return ResponseEntity.ok(b);
    }

    /**
     * 用户上传歌曲的操作
     * @param song
     * @param cids
     * @return
     */
    @PostMapping("/upload/song")
    public ResponseEntity<Void> uploadSong(
            Song song,
            @RequestParam("userId")Long userId,
            @RequestParam("cids")List<Long> cids) {

        userService.uploadSong(song,userId,cids);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }


    @GetMapping("/upload/list")
    public ResponseEntity<List<Song>> upSongList(@RequestParam("userId") Long userId) {
        List<Song> list = userService.getUploadSongByUId(userId);
        return ResponseEntity.ok(list);
    }

}
